package com.lb.shopguide.adapter;

import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ScreenUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.lb.shopguide.GuideApplication;
import com.lb.shopguide.R;
import com.lb.shopguide.entity.MijiBean;
import com.lb.shopguide.util.lb.ClickUtil;
import com.lb.shopguide.util.pic.ImageLoaderUtils;

import java.util.List;

/**
 * Created by kang on 2017/6/7.
 */

public class AdapterMijiList extends BaseQuickAdapter<MijiBean, BaseViewHolder> {
    public AdapterMijiList(int layoutResId, List<MijiBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(final BaseViewHolder helper, final MijiBean item) {
        View view = helper.itemView;
        ImageView ivBack = view.findViewById(R.id.iv_back);
        ImageView ivAvatar = view.findViewById(R.id.iv_avatar);
        TextView tvTutorName = view.findViewById(R.id.tv_tutor_name);
        TextView tvPosition = view.findViewById(R.id.tv_position);
        final CheckBox chkFav = view.findViewById(R.id.chk_fav);
        TextView tvFav = view.findViewById(R.id.tv_fav_num);
        LinearLayout layoutFav = view.findViewById(R.id.layout_like);
        RelativeLayout layoutBottom = view.findViewById(R.id.layout_bottom);

        ViewGroup.LayoutParams params = ivBack.getLayoutParams();
        params.width = (int) (ScreenUtils.getScreenWidth() - 20 * GuideApplication.getAppContext().getResources().getDisplayMetrics().density);
        ivBack.setLayoutParams(params);
//        ivBack.setAdjustViewBounds(true);
        ImageLoaderUtils.getGlideManager().asBitmap().load(item.getEsoCoverPicUrl()).apply(ImageLoaderUtils.getSGCommonGlideOption(8)).into(ivBack);
//        ImageLoaderUtils.displayRound(mContext, ivBack, item.getEsoCoverPicUrl(), 4);
        ImageLoaderUtils.displayCircle(mContext, ivAvatar, item.getTutorPicUrl());
        tvTutorName.setText(item.getTutorName());
//        tvMijiDesc.setText(item.getEsoContent());
        tvPosition.setText(item.getTutorTitle());
        tvFav.setText("" + item.getEsoLikedNum());

        if (helper.getAdapterPosition() % 2 == 0) {
            layoutBottom.setBackgroundResource(R.drawable.bg_round_corner_miji_item_bottom_oushu);
        } else {
            layoutBottom.setBackgroundResource(R.drawable.bg_round_corner_miji_item_bottom_jishu);
        }
        chkFav.setChecked(item.isLiked());
        layoutFav.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!ClickUtil.isFastDoubleClick()) {
                    boolean checked = !chkFav.isChecked();
                    if (mOnLikeClickListener != null) {
                        mOnLikeClickListener.onLikeClick(helper.getAdapterPosition() - getHeaderLayoutCount(), checked);
                    }
                }
            }
        });
//        chkFav.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//                if (mOnLikeClickListener != null) {
//                    mOnLikeClickListener.onLikeClick(helper.getAdapterPosition() - getHeaderLayoutCount(), isChecked);
//                }
//            }
//        });
    }

    public interface OnLikeClickListener {
        void onLikeClick(int pos, boolean checked);
    }

    private OnLikeClickListener mOnLikeClickListener;

    public void setOnLikeClickListener(OnLikeClickListener listener) {
        mOnLikeClickListener = listener;
    }

}
